package com.jieao.template.controller;

import cn.hutool.core.util.IdUtil;
import com.jieao.template.common.util.CurrentlyLoggedInUtil;
import com.jieao.template.common.util.JsonUtils;
import com.jieao.template.entity.permissionMgr.PermissionDO;
import com.jieao.template.entity.pub.JsonData;
import com.jieao.template.entity.pub.Pagination;
import com.jieao.template.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * 权限表(Permission)表控制层
 *
 * @author jzw
 * @since 2020-07-15 15:05:57
 */
@Slf4j
@Validated
@Api(value = "权限表模块接口", tags = "权限表模块接口")
@RestController
@RequestMapping("/api/v1/permission")
public class PermissionController
{
    /**
     * 服务对象
     */
    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value = "权限树", notes = "  权限树")
    @GetMapping("permissionTree")
    JsonData getPermissionTree()
    {
        log.info("获取权限树,  操作人账户:{}", CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok(permissionService.getPermissionTree());
    }

    @DeleteMapping("menuOrPermission")
    @ApiOperation(value = "停启用权限", notes = "  停启用权限")
    JsonData delMenuOrPermission(@ApiParam(name = "id", value = "权限id", required = true) String id)
    {
        permissionService.delMenuOrPermission(id);
        log.info("停启用权限, 参数:{}, 操作人账户:{}", id, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok();
    }

    /**
     * @author jzw
     * @since 2020-07-15 15:05:57
     */
    @GetMapping("/selectOne")
    @ApiOperation(value = "查询单条数据", notes = "通过主键查询Permission表单条数据")
    public JsonData selectOne(@ApiParam(name = "id", value = "Permission表id", required = true) @NotBlank(message = "id不能为空") String id)
    {
        log.info("通过id查询Permission表单条数据, id:{}, 操作人账户:{}", id, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok(permissionService.queryById(id));
    }

    /**
     * @author jzw
     * @since 2020-07-15 15:05:57
     */
    @GetMapping("/queryAll")
    @ApiOperation(value = "查询全部数据", notes = "可通过permissionDO对象包含的参数进行数据筛选")
    public JsonData queryAll(@RequestParam Map<String, Object> params)
    {
        PermissionDO permission = JsonUtils.map2pojo(params, PermissionDO.class);
        log.info("查询Permission表数据, 参数:{}, 操作人账户:{}", permission, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok(permissionService.queryAll(permission));
    }

    /**
     * @author jzw
     * @since 2020-07-15 15:05:57
     */
    @ApiOperation(value = "查询分页数据", notes = "可通过Pagination对象传递分页信息及其他参数进行数据筛选")
    @GetMapping("/queryPageList")
    public JsonData queryPageList(Pagination pagination)
    {
        log.info("分页查询Permission表数据, 参数:{}, 操作人账户:{}", pagination, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok(permissionService.queryAllByLimit(pagination));
    }

    /**
     * @author jzw
     * @since 2020-07-15 15:05:57
     */
    @ApiOperation(value = "新增数据", notes = "新增一条Permission表数据")
    @PostMapping("/insert")
    public JsonData insert(@RequestBody PermissionDO permission)
    {
        permission.setId(IdUtil.simpleUUID());
        permissionService.insert(permission);
        log.info("新增Permission表数据, 参数:{}, 操作人账户:{}", permission, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok();
    }

    /**
     * @author jzw
     * @since 2020-07-15 15:05:57
     */
    @ApiOperation(value = "修改数据", notes = "修改一条Permission表数据")
    @PutMapping("/update")
    public JsonData update(@RequestBody PermissionDO permission)
    {
        permissionService.update(permission);
        log.info("修改Permission表数据, 参数:{}, 操作人账户:{}", permission, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok();
    }

    /**
     * @author jzw
     * @since 2020-07-15 15:05:57
     */
    @ApiOperation(value = "删除数据", notes = "通过主键假删除一条Permission表数据")
    @DeleteMapping("/deleteById")
    public JsonData deleteById(@ApiParam(name = "id", value = "Permission表id", required = true) String id)
    {
        permissionService.deleteById(id);
        log.info("删除单条Permission表数据, 参数:{}, 操作人账户:{}", id, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok();
    }

    /**
     * @author jzw
     * @since 2020-07-15 15:05:57
     */
    @ApiOperation(value = "批量删除数据", notes = "通过主键集合批量假删除Permission表数据")
    @DeleteMapping("/batchDeletionByIdList")
    public JsonData batchDeletionByIdList(@RequestParam List<String> idList)
    {
        permissionService.batchDeletionByIdList(idList);
        log.info("批量删除Permission表数据, 参数:{}, 操作人账户:{}", idList, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok();
    }

}